package com.ztz.services;

import com.ztz.dao.TestDao;
import com.ztz.execl.ReadExcel;
import com.ztz.execl.ReadExcelTest;
import common.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("Testservices")
@Transactional
public class Testservices {
    @Autowired
    private TestDao testDao;
    public String readExcelFile(MultipartFile file) {
        String result = "";
        //创建处理EXCEL的类
        ReadExcelTest readExcel = new ReadExcelTest();
        //解析excel，获取上传的事件单
        List<Map<String, Object>> userList = readExcel.getExcelInfo(file);
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
        ArrayList array=new ArrayList();
        String[] BZ=array.arrayzb();
       // String[] BZ = {"line_No ", "sex", "repse", "arr"};
        for(Map<String, Object> user:userList){
            String repetition="否";
            //2-3
            /*String drawing_No=user.get(BZ[2]).toString();
            String weld_No=user.get(BZ[3]).toString();
            String zh=drawing_No+weld_No;
            List sercList=testDao.sercaList();
            Map map=new HashMap();
            for (int j=0;j<sercList.size();j++){
                map=(Map) sercList.get(j);
                String zf= map.get("zf").toString();
                if(zf.equals(zh)){
                    repetition="是";
                }
            }*/
            StringBuffer str=new StringBuffer();
            for(int i=0;i<BZ.length;i++){
                if(i<=BZ.length-1){
                    str.append(",").append("'").append(user.get(BZ[i])).append("'");
                }else {
                    str.append(user.get(BZ[i]));
                }
            }
            boolean ret = testDao.insertUser(str);
            if(ret == false){
                result = "插入数据库失败";
            }
        }
        if(userList != null && !userList.isEmpty()){
            result = "上传成功";
        }else{
            result = "上传失败";
        }
        return result;
    }
}


